package com.cheyun.netsalev3.view

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.bean.ClueDetails
import com.cheyun.netsalev3.databinding.ActivityEditCarBuyingRecordBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.EditCarBuyingRecordActivityViewModel

class EditCarBuyingRecordActivity : BaseActivity<ActivityEditCarBuyingRecordBinding>() {

    private var activity:Activity? = null

    private lateinit var adpter: MyAdapter
    override fun initContentView(): Int {
        return R.layout.activity_edit_car_buying_record
    }
    var tiCar=false
    override fun initViewModel(): ViewModel? {
        var clueDetails = intent.getSerializableExtra("clueDetails") as ClueDetails
        tiCar = intent.getBooleanExtra("tiCar",false)
        var factory = InjectorUtils.getEditCarBuyingRecordViewModelFactory(clueDetails,tiCar)
        return ViewModelProviders.of(this, factory).get(EditCarBuyingRecordActivityViewModel::class.java)
    }


    override fun initView() {
        activity = this
        adpter = MyAdapter(R.layout.edit_car_item, binding.viewModel)
        binding.recyclerView.adapter = adpter
        binding.cancelBut.setOnClickListener {
            finish()
        }
        if (tiCar){
            binding.headview.titleText?.text="编辑提车记录"
        }
    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adpter.submitList(list)
        })

        binding.viewModel?.editBtnEnable?.observe(this, Observer {
            binding.okBut.isEnabled = it
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 102) {
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                binding.viewModel?.setCarData(carBrandParam)
            }
        }
    }

}
